package com.example.emaildemo.demos.web.controller;

import com.example.emaildemo.demos.web.email.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class EmailController {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private MailService mailService;

    @GetMapping("sendEmail")
    public String sendEmail(@RequestParam("email") String email, HttpServletRequest request) {
        // 发送到旧邮箱
        if (email == null || email == "") {
            // 获取登录用户Id
            String token = (String) request.getServletContext().getAttribute("token");
        }
        int code = 666;
        String content = "验证码：" + code + "\n\r此验证码用于更换邮箱绑定，请勿将验证码告知他人，有效期3分钟，请妥善保管。";
        mailService.sendSimpleMail(from, email, email, "修改邮箱验证码", content);
        request.getServletContext().setAttribute("code", code);
        return "success";
    }

    @GetMapping("send")
    public String send(@RequestParam("email") String email, @RequestParam("msg") String msg, HttpServletRequest request) {
//        int code = 666;
//        String content = "验证码：" + code + "\n\r此验证码用于更换邮箱绑定，请勿将验证码告知他人，有效期3分钟，请妥善保管。";
        String content = msg;
        mailService.sendSimpleMail(from, email, email, "修改邮箱验证码", content);
//        request.getServletContext().setAttribute("code", code);
        return "success";
    }


}
